import service from '@/service.js'
const getBLEDeviceCharacteristics = (dev,success,extparam=[])=>{
	uni.getBLEDeviceCharacteristics({
		//设备id与服务id必须要给,特征值id先不用获取，直接写死
		deviceId:dev.deviceId,
		serviceId:dev.serviceId,
		success: (res) => {
			// console.log('蓝牙连接:' + JSON.stringify(res))
			if (res.characteristics[0].properties.notify) {
				console.log('该特征值可读');
				// console.log('该特征值可读:' + this.characteristicId);
				// uni.readBLECharacteristicValue({
				// 	deviceId,
				// 	serviceId,
				// 	characteristicId: this.characteristicId,
				// 	success: (res) => {
				// 		console.log('读取设备:', res);
				// 	}
				// });
				// serviceId = this.characteristicId;
				// console.log('deviceId',dev.deviceId)
				// console.log('serviceId',dev.serviceId)
				// console.log('characteristicId',dev.characteristicId)
				
				uni.notifyBLECharacteristicValueChange({
					deviceId:dev.deviceId,
					serviceId:dev.serviceId,
					characteristicId: dev.characteristicId,
					state: true, //是否启用notify通知
					success: (res) => {
						console.log('通知启用(notifyBLECharacteristicValueChange)', res);
					},
					fail:(res)=>{
						console.log(res)
					}
				})
				
			}
			// if (res.characteristics[0].properties.write) {
			// 	console.log('该特征值可写');
			// 	this.canWrite = true;
			// 	//调用写
			// 	// this.writeBLECharacteristicValue()
			// }
			//确保对应服务id下的特征值id具备监听数据变化的特性
			// if (res.characteristics[0].properties.notify || res.characteristics[2].properties
			// 	.indicate) {
			// 	uni.notifyBLECharacteristicValueChange({
			// 		deviceId,
			// 		serviceId,
			// 		characteristicId: this.characteristicId,
			// 		state: true, //是否启用notify通知
			// 		success: (res) => {
			// 			console.log('通知启用(notifyBLECharacteristicValueChange)', res);
			// 		}
			// 	})
			// }
		},
		fail(res) {
			console.error('获取蓝牙设备特征值失败(getBLEDeviceCharacteristics)', res)
		}
	})
	// 操作之前先监听，保证第一时间获取数据（value就是特征值当前最新的值）
	//监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification
	uni.onBLECharacteristicValueChange((characteristic) => {

		if(service.getHeadHex(characteristic.value,0,4)==0xF10E0001)
		{
			const dataView = new DataView(characteristic.value)
			// console.log(dataView)
			// let checkCount = 0;
			// //校验
			// for(let i1=0;i1<=16;i1++)
			// {
				
			// 	checkCount+=dataView.getUint8(i1)
			// }
			// console.log('字节的和：',checkCount)
			// if(checkCount%256!=0){
			// 	return false
			// }
				
			// console.log(this.$service.inttoHex(dataView.getUint8(2)))
			let msg = '成功'
			let unit = 0
			let rate = 1 //1字段  小数点
			let mol =0
			//单位
			if(dataView.getUint8(4)==0x04){
				//mmol/l
				unit = 0
			}
			
			if(dataView.getUint8(4)==0x00){
				//mg/ml
				unit = dataView.getUint8(4)
			}
			// console.log(dataView.getUint8(14))
			// console.log(this.$service.inttoHexStr(dataView.getUint8(14)))
			
			mol = dataView.getUint8(14)/10
			success({
					status: 1,
					code:'onBLECharacteristicValueChange',
					data:{deviceId:dev.deviceId,mol:mol,unit:unit,factory:'mycuia'},
					message:'设备测量数据'
				})
			// console.log(mol)
			// this.insertData(this.deviceId,mol,unit)
				
		
		}
		//向设备发送认证A5 56 89 56
		// if (characteristic.characteristicId == '000000F6-0001-0008-0000-0805F9B34FB0111') {
		// 	console.log('向设备发送认证信息')
		// 	this.sendData = 'A5568956'
		// 	this.writeBLECharacteristicValue()
	
		// }
	
	
		//打印手机接收的数据的数组
		// console.log(this.myDataMeasure);
	
	})
}

export default {
	getBLEDeviceCharacteristics
}